package com.qdh.demo7;

import com.qdh.WordCountJob;
import com.qdh.demo1.SplitCountJob;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.Reducer;
import org.apache.hadoop.mapreduce.lib.input.TextInputFormat;
import org.apache.hadoop.mapreduce.lib.output.TextOutputFormat;

import java.io.IOException;

public class SubjectPartitonJob {
    public static void main(String[] args) throws IOException, InterruptedException, ClassNotFoundException {
        //参数非空判断
        if (args.length != 2) {
            System.out.println("参数个数不对");
            System.exit(0);
        }
        // 1. 初始化配置
        Configuration conf = new Configuration();
        conf.set("fs.defaultFS","hdfs://hadoop11:8020");
        //2. 创建job
        Job job = Job.getInstance(conf);
        job.setJarByClass(WordCountJob.class);

        //3. 设置输入格式化工具和输出格式化
        job.setInputFormatClass(TextInputFormat.class);
        job.setOutputFormatClass(TextOutputFormat.class);

        //4. 设置输入路径和输出路径
  /*      TextInputFormat.addInputPath(job,new Path("/mapreduce/demo10"));
        TextOutputFormat.setOutputPath(job,new Path("/mapreduce/demo10/out"));*/
        TextInputFormat.addInputPath(job,new Path(args[0]));
        TextOutputFormat.setOutputPath(job,new Path(args[1]));

        //5. 设置mapper和reducer
        job.setMapperClass(SubjectPartitonMapper.class);
        job.setReducerClass(SubjectPartitonReducer.class);

        // 6. 设置mapper的kv类型和reducer的kv类型
        job.setMapOutputKeyClass(DescIntWritable.class);
        job.setMapOutputValueClass(Subject.class);

        job.setOutputKeyClass(Subject.class);
        job.setOutputValueClass(DescIntWritable.class);
        //设置Redouce分区
        job.setPartitionerClass(SubjectPartition.class);
        job.setNumReduceTasks(4);//reduceTask数量要和分区数量一样。

        // 7. 启动job
        boolean b = job.waitForCompletion(true);
        System.out.println(b);
    }
    //局部计算
    static class SubjectPartitonMapper extends Mapper<LongWritable, Text, DescIntWritable, Subject> {
        @Override
        protected void map(LongWritable key, Text value, Mapper<LongWritable, Text, DescIntWritable, Subject>.Context context) throws IOException, InterruptedException {

            String[] split = value.toString().split("\t");
            context.write(new DescIntWritable(Integer.parseInt(split[2])),
                    new Subject(split[0], split[1]));
        }
    }

    //全局计算
    static class SubjectPartitonReducer extends Reducer<DescIntWritable, Subject, Subject, DescIntWritable> {
        @Override
        protected void reduce(DescIntWritable key, Iterable<Subject> values, Reducer<DescIntWritable, Subject, Subject, DescIntWritable>.Context context) throws IOException, InterruptedException {
            for (Subject value : values) {
                context.write(value,key);
            }
        }
    }
}
